package com.un.ebs.core.metadata;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSONObject;
import com.un.ebs.core.util.WebResult;

import java.lang.reflect.Field;

/**
 * <p>
 *
 * </p>
 *
 * @author liubo
 * @create 2021-06-22 16:29
 */
public abstract class BaseServiceFunctionExt<T, R extends WebResult> {

    public R convert(JSONObject json, Class clazz) {
        R target = (R) ReflectUtil.newInstance(clazz);
        for (Field f : ReflectUtil.getFields(clazz.getClass())) {
            DbField ann = f.getAnnotation(DbField.class);
            if (ann == null) {
                continue;
            }
            ReflectUtil.setFieldValue(target, f.getName(), json.get(ann.name()));
        }
        return target;
    }

    public abstract R exec(T arg);
}
